<div class="tutorial-bundle" data-metadata-json='{"difficulty": "beginner"}' data-tip-sha1="b9b79fa943f7ea2b42a7d0afbd7e9dcb5f7146d3"><div class="front-matter" data-complete-code-text='import pytch
import random


score = 0


class GameBackground(pytch.Stage):
    Backdrops = ["solid-green.png"]

    @pytch.when_green_flag_clicked
    def show_score(self):
        pytch.show_variable(None, "score")

    @pytch.when_stage_clicked
    def missed_fruit(self):
        global score
        score -= 5
        if score &lt; 0:
            score = 0


class Fruit(pytch.Sprite):
    Costumes = ["apple.png", "orange.png"]

    @pytch.when_this_sprite_clicked
    def hit_fruit(self):
        self.hide()

        global score
        score += 1

        pytch.wait_seconds(1)

        appear_x = random.randint(-200, 200)
        appear_y = random.randint(-140, 140)
        self.go_to_xy(appear_x, appear_y)

        new_costume = random.choice(["apple", "orange"])
        self.switch_costume(new_costume)

        self.show()
' data-initial-code-text="import pytch
"><h1>Shoot the fruit</h1><p>We're going to write a game in Python where the player has to click on
fruit to score points.  We'll write the game in a very similar way to
how we'd create it in Scratch.</p><p><img alt="screenshot" src="screenshot.png#img-center"/></p></div><div class="chapter-content"><h2>Create a stage with backdrop</h2><p>The first thing we'll do is to create a <em>Stage</em> for the game.  To keep
things simple, we'll use a solid dark green background.</p><p>In Scratch, there is automatically a Stage for your project.  In
Pytch, we have to create it.  We're going to call it <code>GameBackground</code>,
and we need to say that it's the <code>Stage</code> for our game.</p><p>In this tutorial, we'll show the code we need in boxes like this — the
blue <code>?</code> will show help on what to do:</p><div class="patch-container" data-code-as-of-commit="import pytch


class GameBackground(pytch.Stage):
" data-slug="empty-GameBackground"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>1</pre></td><td class="linenum"><pre>1</pre></td><td><pre>import pytch</pre></td></tr></tbody><tbody class="diff-add" data-added-text="

class GameBackground(pytch.Stage):
"><tr><td class="linenum"></td><td class="linenum"><pre>2</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>3</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>4</pre></td><td><pre>class GameBackground(pytch.Stage):</pre></td></tr></tbody></table></div></div><p>Here, we need to add two blank lines under the <code>import pytch</code> line
which is already there, and then the line starting <code>class
GameBackground</code>.</p><p>Python ignores the blank lines, but they keep our code tidy and easy
to read.</p><p>This code does not yet make sense, so if you click the green flag you
will get an error!  Come back to this <em>Tutorial</em> tab if that happens.</p><p>In Pytch, the stage has <em>backdrops</em>, like in Scratch.  To say what
backdrops we want, we set a variable called <code>Backdrops</code> for the
stage.  If you look in the <em>Images and sounds</em> tab, you'll see an
image called <code>"solid-green.png"</code>.  We'll use this by adding a line of
code to our program:</p><div class="patch-container" data-code-as-of-commit='import pytch


class GameBackground(pytch.Stage):
    Backdrops = ["solid-green.png"]
' data-slug="Backdrop-for-GameBackground"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>2</pre></td><td class="linenum"><pre>2</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>3</pre></td><td class="linenum"><pre>3</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>4</pre></td><td class="linenum"><pre>4</pre></td><td><pre>class GameBackground(pytch.Stage):</pre></td></tr></tbody><tbody class="diff-add" data-added-text='    Backdrops = ["solid-green.png"]
'><tr><td class="linenum"></td><td class="linenum"><pre>5</pre></td><td><pre>    Backdrops = ["solid-green.png"]</pre></td></tr></tbody></table></div></div><p>The square brackets <code>[</code> and <code>]</code> mean that this is a <em>list</em> of
backdrops.  Our stage here only has one backdrop, but we still
need to write <code>Backdrops</code> as a list.</p><p>Try your program now — click the green flag.  You should see the dark
green backdrop appear.</p></div><div class="chapter-content"><h2>Create the fruit</h2><p>Now we'll add a <em>Sprite</em> for the fruit which the player has to shoot.
In Scratch you would click on the "add Sprite" button.  In Python we
add some code to our program.  We'll call our new Sprite <code>Fruit</code>, and
we need to say that it's a Sprite.  This is the code we need:</p><div class="patch-container" data-code-as-of-commit='import pytch


class GameBackground(pytch.Stage):
    Backdrops = ["solid-green.png"]


class Fruit(pytch.Sprite):
' data-slug="empty-Fruit"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>3</pre></td><td class="linenum"><pre>3</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>4</pre></td><td class="linenum"><pre>4</pre></td><td><pre>class GameBackground(pytch.Stage):</pre></td></tr><tr><td class="linenum"><pre>5</pre></td><td class="linenum"><pre>5</pre></td><td><pre>    Backdrops = ["solid-green.png"]</pre></td></tr></tbody><tbody class="diff-add" data-added-text="

class Fruit(pytch.Sprite):
"><tr><td class="linenum"></td><td class="linenum"><pre>6</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>7</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>8</pre></td><td><pre>class Fruit(pytch.Sprite):</pre></td></tr></tbody></table></div></div><p>Just like in Scratch, Sprites in Pytch have <em>costumes</em>.  We set a
variable to the list of costumes we want our Sprite to have.  For now,
we only have one costume, <code>"apple.png"</code>.</p><div class="patch-container" data-code-as-of-commit='import pytch


class GameBackground(pytch.Stage):
    Backdrops = ["solid-green.png"]


class Fruit(pytch.Sprite):
    Costumes = ["apple.png"]
' data-slug="Costume-for-Fruit"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>6</pre></td><td class="linenum"><pre>6</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>7</pre></td><td class="linenum"><pre>7</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>8</pre></td><td class="linenum"><pre>8</pre></td><td><pre>class Fruit(pytch.Sprite):</pre></td></tr></tbody><tbody class="diff-add" data-added-text='    Costumes = ["apple.png"]
'><tr><td class="linenum"></td><td class="linenum"><pre>9</pre></td><td><pre>    Costumes = ["apple.png"]</pre></td></tr></tbody></table></div></div><p>Try your program — if you click on the green flag, you should see an
apple in the middle of the stage.</p></div><div class="chapter-content"><h2>Let the player click the fruit</h2><p>We need to make something happen when the player clicks on the apple —
we'll make the fruit disappear.  In Scratch you would do something
like</p><pre><code class="language-scratch">when this sprite clicked
hide
</code></pre><p>This has two parts:</p><ul>
<li>
<p>We say <em>when</em> we want something to happen using a "hat block" — when
  this sprite clicked.</p>
</li>
<li>
<p>We say <em>what</em> we want to happen by connecting other blocks
  underneath the hat block — the <em>hide</em> block.</p>
</li>
</ul><p>Things work nearly the same in Pytch.  To say <em>when</em> we want something
to happen, we use a special marker in the code:</p><div class="patch-container" data-code-as-of-commit='import pytch


class GameBackground(pytch.Stage):
    Backdrops = ["solid-green.png"]


class Fruit(pytch.Sprite):
    Costumes = ["apple.png"]

    @pytch.when_this_sprite_clicked
' data-slug="Fruit-when-clicked-decorator"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>7</pre></td><td class="linenum"><pre>7</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>8</pre></td><td class="linenum"><pre>8</pre></td><td><pre>class Fruit(pytch.Sprite):</pre></td></tr><tr><td class="linenum"><pre>9</pre></td><td class="linenum"><pre>9</pre></td><td><pre>    Costumes = ["apple.png"]</pre></td></tr></tbody><tbody class="diff-add" data-added-text="
    @pytch.when_this_sprite_clicked
"><tr><td class="linenum"></td><td class="linenum"><pre>10</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>11</pre></td><td><pre>    @pytch.when_this_sprite_clicked</pre></td></tr></tbody></table></div></div><p>In Python, we need to give a name to the section of code we want to
run.  This piece of code will run when the fruit is hit, so we'll call
it <code>hit_fruit</code>.</p><div class="patch-container" data-code-as-of-commit='import pytch


class GameBackground(pytch.Stage):
    Backdrops = ["solid-green.png"]


class Fruit(pytch.Sprite):
    Costumes = ["apple.png"]

    @pytch.when_this_sprite_clicked
    def hit_fruit(self):
' data-slug="hit-fruit-method-def"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>9</pre></td><td class="linenum"><pre>9</pre></td><td><pre>    Costumes = ["apple.png"]</pre></td></tr><tr><td class="linenum"><pre>10</pre></td><td class="linenum"><pre>10</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>11</pre></td><td class="linenum"><pre>11</pre></td><td><pre>    @pytch.when_this_sprite_clicked</pre></td></tr></tbody><tbody class="diff-add" data-added-text="    def hit_fruit(self):
"><tr><td class="linenum"></td><td class="linenum"><pre>12</pre></td><td><pre>    def hit_fruit(self):</pre></td></tr></tbody></table></div></div><p>(The <code>def</code> is short for <em>define</em> — we're about to define the section
of code called <code>hit_fruit</code>.  The <code>self</code> is like a variable which will
refer to the Sprite being clicked.)</p><p>Finally, we say what we want to happen when the Sprite is clicked — we
want it to <em>hide</em>:</p><div class="patch-container" data-code-as-of-commit='import pytch


class GameBackground(pytch.Stage):
    Backdrops = ["solid-green.png"]


class Fruit(pytch.Sprite):
    Costumes = ["apple.png"]

    @pytch.when_this_sprite_clicked
    def hit_fruit(self):
        self.hide()
' data-slug="hide-when-hit"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>10</pre></td><td class="linenum"><pre>10</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>11</pre></td><td class="linenum"><pre>11</pre></td><td><pre>    @pytch.when_this_sprite_clicked</pre></td></tr><tr><td class="linenum"><pre>12</pre></td><td class="linenum"><pre>12</pre></td><td><pre>    def hit_fruit(self):</pre></td></tr></tbody><tbody class="diff-add" data-added-text="        self.hide()
"><tr><td class="linenum"></td><td class="linenum"><pre>13</pre></td><td><pre>        self.hide()</pre></td></tr></tbody></table></div></div><p>The <code>self</code> means that we want <em>this</em> Sprite to hide.</p><p>When you have added the code to your program, test it!  Click the
green flag and check that:</p><ul>
<li>the apple disappears when you click on it;</li>
<li>the apple does <em>not</em> disappear when you click somewhere random on
  the green background.</li>
</ul></div><div class="chapter-content"><h2>Make the apple reappear after being hit</h2><p>The game is not very exciting yet.  Once the player clicks on the
apple, it disappears, and that's it.  We need to make the apple
reappear so the player can keep playing.</p><p>After the fruit has hidden itself, we want it to wait for a short
time, and then show itself again.  In Scratch, we would add these
blocks to the bottom of the 'when this sprite clicked' script:</p><pre><code class="language-scratch">wait [1] seconds
show
</code></pre><p>In Python, we add two lines to our code:</p><div class="patch-container" data-code-as-of-commit='import pytch


class GameBackground(pytch.Stage):
    Backdrops = ["solid-green.png"]


class Fruit(pytch.Sprite):
    Costumes = ["apple.png"]

    @pytch.when_this_sprite_clicked
    def hit_fruit(self):
        self.hide()
        pytch.wait_seconds(1)
        self.show()
' data-slug="wait-then-show"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>11</pre></td><td class="linenum"><pre>11</pre></td><td><pre>    @pytch.when_this_sprite_clicked</pre></td></tr><tr><td class="linenum"><pre>12</pre></td><td class="linenum"><pre>12</pre></td><td><pre>    def hit_fruit(self):</pre></td></tr><tr><td class="linenum"><pre>13</pre></td><td class="linenum"><pre>13</pre></td><td><pre>        self.hide()</pre></td></tr></tbody><tbody class="diff-add" data-added-text="        pytch.wait_seconds(1)
        self.show()
"><tr><td class="linenum"></td><td class="linenum"><pre>14</pre></td><td><pre>        pytch.wait_seconds(1)</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>15</pre></td><td><pre>        self.show()</pre></td></tr></tbody></table></div></div><p>Try the program now!  The apple should disappear when you click it,
then reappear one second later.</p></div><div class="chapter-content"><h2>Make the apple reappear somewhere else</h2><p>The game is better now, but still not very exciting.  The player knows
where the apple is going to appear.  We want the apple to reappear at
a random place on on the stage.</p><h3>Saying where to go on the stage</h3><p>Like in Scratch, positions on the Pytch stage are described by two
numbers, <code>x</code> and <code>y</code>:</p><ul>
<li>The <code>x</code> number says how far right of the centre we are (and so <code>x</code>
  is negative if we are <em>left</em> of the centre).</li>
<li>The left-right centre of the stage is <code>x = 0</code>, far left is <code>x =
  -240</code>, and far right is <code>x = 240</code>.</li>
<li>The <code>y</code> number says how far above the centre we are (and so <code>y</code> is
  negative if we are <em>below</em> the centre).</li>
<li>The up-down centre of the stage is <code>y = 0</code>, the very bottom is <code>y =
  -180</code>, and the very top is <code>y = 180</code>.</li>
</ul><h3>Picking a random number</h3><p>We'll need to randomly pick <code>x</code> and <code>y</code> numbers for where the apple
reappears.</p><p>To do this, we will use some code that the Python team have written
which picks random numbers.  We first need to say that we want to use
this code.  In Scratch, we can "add an extension" to be able to use
more blocks.  In Python, we use an <em>import</em> statement:</p><div class="patch-container" data-code-as-of-commit='import pytch
import random


class GameBackground(pytch.Stage):
    Backdrops = ["solid-green.png"]


class Fruit(pytch.Sprite):
    Costumes = ["apple.png"]

    @pytch.when_this_sprite_clicked
    def hit_fruit(self):
        self.hide()
        pytch.wait_seconds(1)
        self.show()
' data-slug="import-random"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>1</pre></td><td class="linenum"><pre>1</pre></td><td><pre>import pytch</pre></td></tr></tbody><tbody class="diff-add" data-added-text="import random
"><tr><td class="linenum"></td><td class="linenum"><pre>2</pre></td><td><pre>import random</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>2</pre></td><td class="linenum"><pre>3</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>3</pre></td><td class="linenum"><pre>4</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>4</pre></td><td class="linenum"><pre>5</pre></td><td><pre>class GameBackground(pytch.Stage):</pre></td></tr></tbody></table></div></div><p>(You might notice that our program starts off with <code>import pytch</code> —
this is what tells Python that we want to be able to use things like
<code>pytch.wait_seconds</code>.)</p><p>We will use the <code>randint</code> 'function' of this <code>random</code> code, to pick
random numbers for us, and choose where the apple will reappear.  We
will need to tell <code>randint</code> the smallest and biggest possible numbers
we want.</p><p>In Scratch you would put this information into the 'holes' of
the block.  For example, to roll a dice you might say:</p><pre><code class="language-scratch">pick random [1] to [6]
</code></pre><p>In Python, we put the pieces of information between brackets <code>(</code> and
<code>)</code>.  This Scratch block would look like this in Python:</p><pre><code>random.randint(1, 6)
</code></pre><h3>Picking a random place to appear</h3><p>We want to pick a random <code>x</code> number and a random <code>y</code> number to say
where the apple should reappear.  We will store these values in
<em>variables</em>.  These work very much like Scratch variables.  In Python,
you don't need to explicitly 'make a variable' — you just set a
variable to something and it is created for you.</p><p>We will make a variable called <code>appear_x</code> to hold the <code>x</code> number for
where we want the apple to reappear:</p><div class="patch-container" data-code-as-of-commit='import pytch
import random


class GameBackground(pytch.Stage):
    Backdrops = ["solid-green.png"]


class Fruit(pytch.Sprite):
    Costumes = ["apple.png"]

    @pytch.when_this_sprite_clicked
    def hit_fruit(self):
        self.hide()
        pytch.wait_seconds(1)

        appear_x = random.randint(-200, 200)

        self.show()
' data-slug="assign-new-x"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>13</pre></td><td class="linenum"><pre>13</pre></td><td><pre>    def hit_fruit(self):</pre></td></tr><tr><td class="linenum"><pre>14</pre></td><td class="linenum"><pre>14</pre></td><td><pre>        self.hide()</pre></td></tr><tr><td class="linenum"><pre>15</pre></td><td class="linenum"><pre>15</pre></td><td><pre>        pytch.wait_seconds(1)</pre></td></tr></tbody><tbody class="diff-add" data-added-text="
        appear_x = random.randint(-200, 200)

"><tr><td class="linenum"></td><td class="linenum"><pre>16</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>17</pre></td><td><pre>        appear_x = random.randint(-200, 200)</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>18</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>16</pre></td><td class="linenum"><pre>19</pre></td><td><pre>        self.show()</pre></td></tr></tbody></table></div></div><p>The <code>-200</code> and <code>200</code> don't go all the way to the edges of the stage,
because we want the apple to always definitely be completely on the
stage — it's the centre of the apple which we are picking a location
for.</p><p>We will set another variable, <code>appear_y</code>, to hold the <code>y</code> number:</p><div class="patch-container" data-code-as-of-commit='import pytch
import random


class GameBackground(pytch.Stage):
    Backdrops = ["solid-green.png"]


class Fruit(pytch.Sprite):
    Costumes = ["apple.png"]

    @pytch.when_this_sprite_clicked
    def hit_fruit(self):
        self.hide()
        pytch.wait_seconds(1)

        appear_x = random.randint(-200, 200)
        appear_y = random.randint(-140, 140)

        self.show()
' data-slug="assign-new-y"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>15</pre></td><td class="linenum"><pre>15</pre></td><td><pre>        pytch.wait_seconds(1)</pre></td></tr><tr><td class="linenum"><pre>16</pre></td><td class="linenum"><pre>16</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>17</pre></td><td class="linenum"><pre>17</pre></td><td><pre>        appear_x = random.randint(-200, 200)</pre></td></tr></tbody><tbody class="diff-add" data-added-text="        appear_y = random.randint(-140, 140)
"><tr><td class="linenum"></td><td class="linenum"><pre>18</pre></td><td><pre>        appear_y = random.randint(-140, 140)</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>18</pre></td><td class="linenum"><pre>19</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>19</pre></td><td class="linenum"><pre>20</pre></td><td><pre>        self.show()</pre></td></tr></tbody></table></div></div><p>We use <code>-140</code> and <code>140</code> rather than <code>-180</code> and <code>180</code> to make sure the
apple doesn't go off the top or off the bottom of the stage.</p><h3>Appear at the random location</h3><p>Now we know where we want the apple to randomly reappear, we can make
it go there just before showing itself.</p><p>In Scratch, we can use variables to 'fill in holes' in blocks, like:</p><pre><code class="language-scratch">go to x: (appear_x) y: (appear_y)
</code></pre><p>In Python, we put the variables in <code>()</code>s:</p><div class="patch-container" data-code-as-of-commit='import pytch
import random


class GameBackground(pytch.Stage):
    Backdrops = ["solid-green.png"]


class Fruit(pytch.Sprite):
    Costumes = ["apple.png"]

    @pytch.when_this_sprite_clicked
    def hit_fruit(self):
        self.hide()
        pytch.wait_seconds(1)

        appear_x = random.randint(-200, 200)
        appear_y = random.randint(-140, 140)
        self.go_to_xy(appear_x, appear_y)

        self.show()
' data-slug="go-to-random-x-y"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>16</pre></td><td class="linenum"><pre>16</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>17</pre></td><td class="linenum"><pre>17</pre></td><td><pre>        appear_x = random.randint(-200, 200)</pre></td></tr><tr><td class="linenum"><pre>18</pre></td><td class="linenum"><pre>18</pre></td><td><pre>        appear_y = random.randint(-140, 140)</pre></td></tr></tbody><tbody class="diff-add" data-added-text="        self.go_to_xy(appear_x, appear_y)
"><tr><td class="linenum"></td><td class="linenum"><pre>19</pre></td><td><pre>        self.go_to_xy(appear_x, appear_y)</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>19</pre></td><td class="linenum"><pre>20</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>20</pre></td><td class="linenum"><pre>21</pre></td><td><pre>        self.show()</pre></td></tr></tbody></table></div></div><h3>Try it!</h3><p>Run your program with the green flag.  The apple should reappear in a
random place every time you click it.</p></div><div class="chapter-content"><h2>Show the player's score</h2><p>It would be good if the player knew how well they were doing.  We want
to keep a <em>score</em>, and give the player a point every time they shoot
the fruit.</p><p>We will set this variable at the top of our program.  When the game
starts, the player has no points, so we set our new <code>score</code> variable
to zero.  In Scratch we would say</p><pre><code class="language-scratch">set [score v] to [0]
</code></pre><p>and in Python, we use this code, which creates the variable and sets
it to zero:</p><div class="patch-container" data-code-as-of-commit='import pytch
import random


score = 0


class GameBackground(pytch.Stage):
    Backdrops = ["solid-green.png"]


class Fruit(pytch.Sprite):
    Costumes = ["apple.png"]

    @pytch.when_this_sprite_clicked
    def hit_fruit(self):
        self.hide()
        pytch.wait_seconds(1)

        appear_x = random.randint(-200, 200)
        appear_y = random.randint(-140, 140)
        self.go_to_xy(appear_x, appear_y)

        self.show()
' data-slug="define-score-global"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>2</pre></td><td class="linenum"><pre>2</pre></td><td><pre>import random</pre></td></tr><tr><td class="linenum"><pre>3</pre></td><td class="linenum"><pre>3</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>4</pre></td><td class="linenum"><pre>4</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-add" data-added-text="score = 0


"><tr><td class="linenum"></td><td class="linenum"><pre>5</pre></td><td><pre>score = 0</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>6</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>7</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>5</pre></td><td class="linenum"><pre>8</pre></td><td><pre>class GameBackground(pytch.Stage):</pre></td></tr><tr><td class="linenum"><pre>6</pre></td><td class="linenum"><pre>9</pre></td><td><pre>    Backdrops = ["solid-green.png"]</pre></td></tr><tr><td class="linenum"><pre>7</pre></td><td class="linenum"><pre>10</pre></td><td><pre></pre></td></tr></tbody></table></div></div><h3>Showing the score</h3><p>In Scratch you can just tick a box to show a variable on the stage.
In Pytch we write code to do this.  We want to do this as soon as the
green flag is clicked, so we'll need code to act like Scratch's "when
green flag clicked" hat block.  We'll put this in the <code>GameBackground</code>
(our stage), because it's part of the overall game.</p><div class="patch-container" data-code-as-of-commit='import pytch
import random


score = 0


class GameBackground(pytch.Stage):
    Backdrops = ["solid-green.png"]

    @pytch.when_green_flag_clicked


class Fruit(pytch.Sprite):
    Costumes = ["apple.png"]

    @pytch.when_this_sprite_clicked
    def hit_fruit(self):
        self.hide()
        pytch.wait_seconds(1)

        appear_x = random.randint(-200, 200)
        appear_y = random.randint(-140, 140)
        self.go_to_xy(appear_x, appear_y)

        self.show()
' data-slug="GameBackground-when-green-flag-decorator"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>8</pre></td><td class="linenum"><pre>8</pre></td><td><pre>class GameBackground(pytch.Stage):</pre></td></tr><tr><td class="linenum"><pre>9</pre></td><td class="linenum"><pre>9</pre></td><td><pre>    Backdrops = ["solid-green.png"]</pre></td></tr><tr><td class="linenum"><pre>10</pre></td><td class="linenum"><pre>10</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-add" data-added-text="    @pytch.when_green_flag_clicked

"><tr><td class="linenum"></td><td class="linenum"><pre>11</pre></td><td><pre>    @pytch.when_green_flag_clicked</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>12</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>11</pre></td><td class="linenum"><pre>13</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>12</pre></td><td class="linenum"><pre>14</pre></td><td><pre>class Fruit(pytch.Sprite):</pre></td></tr><tr><td class="linenum"><pre>13</pre></td><td class="linenum"><pre>15</pre></td><td><pre>    Costumes = ["apple.png"]</pre></td></tr></tbody></table></div></div><p>Showing a variable in Pytch uses a couple of more advanced ideas, so
for this tutorial, we won't go into the details.  The code we need is:</p><div class="patch-container" data-code-as-of-commit='import pytch
import random


score = 0


class GameBackground(pytch.Stage):
    Backdrops = ["solid-green.png"]

    @pytch.when_green_flag_clicked
    def show_score(self):
        pytch.show_variable(None, "score")


class Fruit(pytch.Sprite):
    Costumes = ["apple.png"]

    @pytch.when_this_sprite_clicked
    def hit_fruit(self):
        self.hide()
        pytch.wait_seconds(1)

        appear_x = random.randint(-200, 200)
        appear_y = random.randint(-140, 140)
        self.go_to_xy(appear_x, appear_y)

        self.show()
' data-slug="show-score"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>9</pre></td><td class="linenum"><pre>9</pre></td><td><pre>    Backdrops = ["solid-green.png"]</pre></td></tr><tr><td class="linenum"><pre>10</pre></td><td class="linenum"><pre>10</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>11</pre></td><td class="linenum"><pre>11</pre></td><td><pre>    @pytch.when_green_flag_clicked</pre></td></tr></tbody><tbody class="diff-add" data-added-text='    def show_score(self):
        pytch.show_variable(None, "score")
'><tr><td class="linenum"></td><td class="linenum"><pre>12</pre></td><td><pre>    def show_score(self):</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>13</pre></td><td><pre>        pytch.show_variable(None, "score")</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>12</pre></td><td class="linenum"><pre>14</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>13</pre></td><td class="linenum"><pre>15</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>14</pre></td><td class="linenum"><pre>16</pre></td><td><pre>class Fruit(pytch.Sprite):</pre></td></tr></tbody></table></div></div><p>So far our program has no way for the player to get points, so the
score will be stuck at zero.  We will fix this next.</p></div><div class="chapter-content"><h2>Give the player points</h2><p>To increase the player's score, in Scratch we would do</p><pre><code class="language-scratch">change [score v] by [1]
</code></pre><p>Python has <code>+=</code> ('change by adding'), which works very much the same.
The code we want is</p><pre><code>score += 1
</code></pre><p>and it needs to go in our code which runs when the <code>Fruit</code> is clicked.
There is a detail we need to deal with first, which is to tell Python
that we want to change the <em>global</em> variable <code>score</code>.  A 'global'
variable is very like a "for all sprites" variable in Scratch.  Add
these lines to the <code>hit_fruit</code> code:</p><div class="patch-container" data-code-as-of-commit='import pytch
import random


score = 0


class GameBackground(pytch.Stage):
    Backdrops = ["solid-green.png"]

    @pytch.when_green_flag_clicked
    def show_score(self):
        pytch.show_variable(None, "score")


class Fruit(pytch.Sprite):
    Costumes = ["apple.png"]

    @pytch.when_this_sprite_clicked
    def hit_fruit(self):
        self.hide()

        global score
        score += 1

        pytch.wait_seconds(1)

        appear_x = random.randint(-200, 200)
        appear_y = random.randint(-140, 140)
        self.go_to_xy(appear_x, appear_y)

        self.show()
' data-slug="award-point-when-hit"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>19</pre></td><td class="linenum"><pre>19</pre></td><td><pre>    @pytch.when_this_sprite_clicked</pre></td></tr><tr><td class="linenum"><pre>20</pre></td><td class="linenum"><pre>20</pre></td><td><pre>    def hit_fruit(self):</pre></td></tr><tr><td class="linenum"><pre>21</pre></td><td class="linenum"><pre>21</pre></td><td><pre>        self.hide()</pre></td></tr></tbody><tbody class="diff-add" data-added-text="
        global score
        score += 1

"><tr><td class="linenum"></td><td class="linenum"><pre>22</pre></td><td><pre></pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>23</pre></td><td><pre>        global score</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>24</pre></td><td><pre>        score += 1</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>25</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>22</pre></td><td class="linenum"><pre>26</pre></td><td><pre>        pytch.wait_seconds(1)</pre></td></tr><tr><td class="linenum"><pre>23</pre></td><td class="linenum"><pre>27</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>24</pre></td><td class="linenum"><pre>28</pre></td><td><pre>        appear_x = random.randint(-200, 200)</pre></td></tr></tbody></table></div></div><p>Try it!  The score should now count how many times the player has
hit the fruit.</p></div><div class="chapter-content"><h2>Knock off points if player misses</h2><p>To make the game more challenging, we'll make it so if the player
misses the fruit, they lose points.</p><p>We can tell if the player misses by making our stage run some code
when <em>it</em> is clicked.  We'll start by adding code to act like
Scratch's "when stage clicked" hat block:</p><div class="patch-container" data-code-as-of-commit='import pytch
import random


score = 0


class GameBackground(pytch.Stage):
    Backdrops = ["solid-green.png"]

    @pytch.when_green_flag_clicked
    def show_score(self):
        pytch.show_variable(None, "score")

    @pytch.when_stage_clicked


class Fruit(pytch.Sprite):
    Costumes = ["apple.png"]

    @pytch.when_this_sprite_clicked
    def hit_fruit(self):
        self.hide()

        global score
        score += 1

        pytch.wait_seconds(1)

        appear_x = random.randint(-200, 200)
        appear_y = random.randint(-140, 140)
        self.go_to_xy(appear_x, appear_y)

        self.show()
' data-slug="when-stage-clicked-decorator"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>12</pre></td><td class="linenum"><pre>12</pre></td><td><pre>    def show_score(self):</pre></td></tr><tr><td class="linenum"><pre>13</pre></td><td class="linenum"><pre>13</pre></td><td><pre>        pytch.show_variable(None, "score")</pre></td></tr><tr><td class="linenum"><pre>14</pre></td><td class="linenum"><pre>14</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-add" data-added-text="    @pytch.when_stage_clicked

"><tr><td class="linenum"></td><td class="linenum"><pre>15</pre></td><td><pre>    @pytch.when_stage_clicked</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>16</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>15</pre></td><td class="linenum"><pre>17</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>16</pre></td><td class="linenum"><pre>18</pre></td><td><pre>class Fruit(pytch.Sprite):</pre></td></tr><tr><td class="linenum"><pre>17</pre></td><td class="linenum"><pre>19</pre></td><td><pre>    Costumes = ["apple.png"]</pre></td></tr></tbody></table></div></div><p>We need a name for the chunk of code we're about to write.  The code
will run when the player misses the fruit, so <code>missed_fruit</code> is a good
name:</p><div class="patch-container" data-code-as-of-commit='import pytch
import random


score = 0


class GameBackground(pytch.Stage):
    Backdrops = ["solid-green.png"]

    @pytch.when_green_flag_clicked
    def show_score(self):
        pytch.show_variable(None, "score")

    @pytch.when_stage_clicked
    def missed_fruit(self):


class Fruit(pytch.Sprite):
    Costumes = ["apple.png"]

    @pytch.when_this_sprite_clicked
    def hit_fruit(self):
        self.hide()

        global score
        score += 1

        pytch.wait_seconds(1)

        appear_x = random.randint(-200, 200)
        appear_y = random.randint(-140, 140)
        self.go_to_xy(appear_x, appear_y)

        self.show()
' data-slug="missed-fruit-def"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>13</pre></td><td class="linenum"><pre>13</pre></td><td><pre>        pytch.show_variable(None, "score")</pre></td></tr><tr><td class="linenum"><pre>14</pre></td><td class="linenum"><pre>14</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>15</pre></td><td class="linenum"><pre>15</pre></td><td><pre>    @pytch.when_stage_clicked</pre></td></tr></tbody><tbody class="diff-add" data-added-text="    def missed_fruit(self):
"><tr><td class="linenum"></td><td class="linenum"><pre>16</pre></td><td><pre>    def missed_fruit(self):</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>16</pre></td><td class="linenum"><pre>17</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>17</pre></td><td class="linenum"><pre>18</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>18</pre></td><td class="linenum"><pre>19</pre></td><td><pre>class Fruit(pytch.Sprite):</pre></td></tr></tbody></table></div></div><p>And now we can knock off points if a click gets through to the stage.
Just as when we gave the player points, we need to tell Python that we
want to change the global <code>score</code> variable.  And this time, instead of
<em>adding</em> to <code>score</code> by using <code>+=</code>, we want to <em>subtract</em> from <code>score</code>,
so we use <code>-=</code> ('change by subtracting').  This code knocks off five
points when the player misses:</p><div class="patch-container" data-code-as-of-commit='import pytch
import random


score = 0


class GameBackground(pytch.Stage):
    Backdrops = ["solid-green.png"]

    @pytch.when_green_flag_clicked
    def show_score(self):
        pytch.show_variable(None, "score")

    @pytch.when_stage_clicked
    def missed_fruit(self):
        global score
        score -= 5


class Fruit(pytch.Sprite):
    Costumes = ["apple.png"]

    @pytch.when_this_sprite_clicked
    def hit_fruit(self):
        self.hide()

        global score
        score += 1

        pytch.wait_seconds(1)

        appear_x = random.randint(-200, 200)
        appear_y = random.randint(-140, 140)
        self.go_to_xy(appear_x, appear_y)

        self.show()
' data-slug="lose-points-on-miss"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>14</pre></td><td class="linenum"><pre>14</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>15</pre></td><td class="linenum"><pre>15</pre></td><td><pre>    @pytch.when_stage_clicked</pre></td></tr><tr><td class="linenum"><pre>16</pre></td><td class="linenum"><pre>16</pre></td><td><pre>    def missed_fruit(self):</pre></td></tr></tbody><tbody class="diff-add" data-added-text="        global score
        score -= 5
"><tr><td class="linenum"></td><td class="linenum"><pre>17</pre></td><td><pre>        global score</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>18</pre></td><td><pre>        score -= 5</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>17</pre></td><td class="linenum"><pre>19</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>18</pre></td><td class="linenum"><pre>20</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>19</pre></td><td class="linenum"><pre>21</pre></td><td><pre>class Fruit(pytch.Sprite):</pre></td></tr></tbody></table></div></div><h3>Stopping the score going negative</h3><p>There is a problem with this: What happens if the player misses when
their score is less than five?  Try it — run your game and
deliberately miss with your first click.  You'll see that the game
says your score is <code>-5</code>.  This is not very sensible.</p><p>After knocking off five points with <code>score -= 5</code>, we want to test
whether the score has become negative.  If so, we want to set <code>score</code>
to zero.  Python has an <code>if</code> statement which works the same as
Scratch's <em>if</em> block.  This is the code we need:</p><div class="patch-container" data-code-as-of-commit='import pytch
import random


score = 0


class GameBackground(pytch.Stage):
    Backdrops = ["solid-green.png"]

    @pytch.when_green_flag_clicked
    def show_score(self):
        pytch.show_variable(None, "score")

    @pytch.when_stage_clicked
    def missed_fruit(self):
        global score
        score -= 5
        if score &lt; 0:
            score = 0


class Fruit(pytch.Sprite):
    Costumes = ["apple.png"]

    @pytch.when_this_sprite_clicked
    def hit_fruit(self):
        self.hide()

        global score
        score += 1

        pytch.wait_seconds(1)

        appear_x = random.randint(-200, 200)
        appear_y = random.randint(-140, 140)
        self.go_to_xy(appear_x, appear_y)

        self.show()
' data-slug="clamp-score-at-zero"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>16</pre></td><td class="linenum"><pre>16</pre></td><td><pre>    def missed_fruit(self):</pre></td></tr><tr><td class="linenum"><pre>17</pre></td><td class="linenum"><pre>17</pre></td><td><pre>        global score</pre></td></tr><tr><td class="linenum"><pre>18</pre></td><td class="linenum"><pre>18</pre></td><td><pre>        score -= 5</pre></td></tr></tbody><tbody class="diff-add" data-added-text="        if score &lt; 0:
            score = 0
"><tr><td class="linenum"></td><td class="linenum"><pre>19</pre></td><td><pre>        if score &lt; 0:</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>20</pre></td><td><pre>            score = 0</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>19</pre></td><td class="linenum"><pre>21</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>20</pre></td><td class="linenum"><pre>22</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>21</pre></td><td class="linenum"><pre>23</pre></td><td><pre>class Fruit(pytch.Sprite):</pre></td></tr></tbody></table></div></div><p>The <code>score &lt; 0</code> part is the test — it asks whether the <code>score</code>
variable is less than zero.  If it is, the <em>indented</em> (pushed to the
right) code runs.  Here, there is only one indented line, which sets
<code>score</code> to zero.</p></div><div class="chapter-content"><h2>Game complete!</h2><p>This is now a playable game written in Python — congratulations!</p><p>Play a few games and see how quickly you can score points.</p></div><div class="chapter-content"><h2>Extra: Add another fruit</h2><p>To make the game look more interesting, we can add another fruit.</p><h3>Add a costume</h3><p>At the moment, the <code>Fruit</code> sprite only has one costume.  We want to
add another one.  There is an image <code>"orange.png"</code> included with this
tutorial, so we just need to tell Pytch to use it, by adding it to the
Fruit's <code>Costumes</code> list.  In Python, the things in a list are written
with commas between them.  So we need to change the line which sets
the Fruit's costumes:</p><div class="patch-container" data-code-as-of-commit='import pytch
import random


score = 0


class GameBackground(pytch.Stage):
    Backdrops = ["solid-green.png"]

    @pytch.when_green_flag_clicked
    def show_score(self):
        pytch.show_variable(None, "score")

    @pytch.when_stage_clicked
    def missed_fruit(self):
        global score
        score -= 5
        if score &lt; 0:
            score = 0


class Fruit(pytch.Sprite):
    Costumes = ["apple.png", "orange.png"]

    @pytch.when_this_sprite_clicked
    def hit_fruit(self):
        self.hide()

        global score
        score += 1

        pytch.wait_seconds(1)

        appear_x = random.randint(-200, 200)
        appear_y = random.randint(-140, 140)
        self.go_to_xy(appear_x, appear_y)

        self.show()
' data-slug="add-orange-costume"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>21</pre></td><td class="linenum"><pre>21</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>22</pre></td><td class="linenum"><pre>22</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>23</pre></td><td class="linenum"><pre>23</pre></td><td><pre>class Fruit(pytch.Sprite):</pre></td></tr></tbody><tbody class="diff-del"><tr><td class="linenum"><pre>24</pre></td><td class="linenum"></td><td><pre>    Costumes = ["apple.png"]</pre></td></tr></tbody><tbody class="diff-add" data-added-text='    Costumes = ["apple.png", "orange.png"]
'><tr><td class="linenum"></td><td class="linenum"><pre>24</pre></td><td><pre>    Costumes = ["apple.png", "orange.png"]</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>25</pre></td><td class="linenum"><pre>25</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>26</pre></td><td class="linenum"><pre>26</pre></td><td><pre>    @pytch.when_this_sprite_clicked</pre></td></tr><tr><td class="linenum"><pre>27</pre></td><td class="linenum"><pre>27</pre></td><td><pre>    def hit_fruit(self):</pre></td></tr></tbody></table></div></div><p>The box explains this change as deleting the <code>Costumes</code> line and
adding a replacement, but you can just type in the new part</p><pre><code>, "orange.png"
</code></pre><p>if that's easier for you.</p><h3>Choose a random costume</h3><p>We have already used the Python function <code>random.randint</code> to pick
random numbers for where to appear on the screen.  There is another
function — <code>random.choice</code> — which chooses a random thing from a
list.  We'll use this to set a variable to the name of the costume we
want the Fruit to wear when it reappears.</p><p>Find the right place in <code>hit_fruit</code> to add this code:</p><div class="patch-container" data-code-as-of-commit='import pytch
import random


score = 0


class GameBackground(pytch.Stage):
    Backdrops = ["solid-green.png"]

    @pytch.when_green_flag_clicked
    def show_score(self):
        pytch.show_variable(None, "score")

    @pytch.when_stage_clicked
    def missed_fruit(self):
        global score
        score -= 5
        if score &lt; 0:
            score = 0


class Fruit(pytch.Sprite):
    Costumes = ["apple.png", "orange.png"]

    @pytch.when_this_sprite_clicked
    def hit_fruit(self):
        self.hide()

        global score
        score += 1

        pytch.wait_seconds(1)

        appear_x = random.randint(-200, 200)
        appear_y = random.randint(-140, 140)
        self.go_to_xy(appear_x, appear_y)

        new_costume = random.choice(["apple", "orange"])

        self.show()
' data-slug="choose-random-new-costume"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>36</pre></td><td class="linenum"><pre>36</pre></td><td><pre>        appear_y = random.randint(-140, 140)</pre></td></tr><tr><td class="linenum"><pre>37</pre></td><td class="linenum"><pre>37</pre></td><td><pre>        self.go_to_xy(appear_x, appear_y)</pre></td></tr><tr><td class="linenum"><pre>38</pre></td><td class="linenum"><pre>38</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-add" data-added-text='        new_costume = random.choice(["apple", "orange"])

'><tr><td class="linenum"></td><td class="linenum"><pre>39</pre></td><td><pre>        new_costume = random.choice(["apple", "orange"])</pre></td></tr><tr><td class="linenum"></td><td class="linenum"><pre>40</pre></td><td><pre></pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>39</pre></td><td class="linenum"><pre>41</pre></td><td><pre>        self.show()</pre></td></tr></tbody></table></div></div><h3>Switch to the chosen costume</h3><p>And just like Scratch has a <em>switch costume to</em> block, Pytch has a way
to get a Sprite to wear a different costume.  We'll use this to switch
to the costume we just randomly picked:</p><div class="patch-container" data-code-as-of-commit='import pytch
import random


score = 0


class GameBackground(pytch.Stage):
    Backdrops = ["solid-green.png"]

    @pytch.when_green_flag_clicked
    def show_score(self):
        pytch.show_variable(None, "score")

    @pytch.when_stage_clicked
    def missed_fruit(self):
        global score
        score -= 5
        if score &lt; 0:
            score = 0


class Fruit(pytch.Sprite):
    Costumes = ["apple.png", "orange.png"]

    @pytch.when_this_sprite_clicked
    def hit_fruit(self):
        self.hide()

        global score
        score += 1

        pytch.wait_seconds(1)

        appear_x = random.randint(-200, 200)
        appear_y = random.randint(-140, 140)
        self.go_to_xy(appear_x, appear_y)

        new_costume = random.choice(["apple", "orange"])
        self.switch_costume(new_costume)

        self.show()
' data-slug="switch-to-random-costume"><div class="patch"><table><tbody class="diff-unch"><tr><td class="linenum"><pre>37</pre></td><td class="linenum"><pre>37</pre></td><td><pre>        self.go_to_xy(appear_x, appear_y)</pre></td></tr><tr><td class="linenum"><pre>38</pre></td><td class="linenum"><pre>38</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>39</pre></td><td class="linenum"><pre>39</pre></td><td><pre>        new_costume = random.choice(["apple", "orange"])</pre></td></tr></tbody><tbody class="diff-add" data-added-text="        self.switch_costume(new_costume)
"><tr><td class="linenum"></td><td class="linenum"><pre>40</pre></td><td><pre>        self.switch_costume(new_costume)</pre></td></tr></tbody><tbody class="diff-unch"><tr><td class="linenum"><pre>40</pre></td><td class="linenum"><pre>41</pre></td><td><pre></pre></td></tr><tr><td class="linenum"><pre>41</pre></td><td class="linenum"><pre>42</pre></td><td><pre>        self.show()</pre></td></tr></tbody></table></div></div></div><div class="chapter-content"><h2>Challenges</h2><p>Can you change your program to solve these challenges?</p><ul>
<li>
<p>Give the player 10 points for every fruit they hit instead of 1.</p>
</li>
<li>
<p>Give the player 5 points for an apple and 10 points for an orange.
  (<em>Hint:</em> You can use the code <code>self.costume_name</code> to find out what
  costume the sprite is currently wearing.)</p>
</li>
<li>
<p>Make the fruit smaller, so it's harder to hit.  (<em>Hint:</em> You can use
  the code <code>self.set_size(0.5)</code> to shrink a Sprite to half its normal
  size.)</p>
</li>
<li>
<p>Harder challenge: Make the fruit smaller if the player has more
  points.  This makes the game get more difficult as the player gets
  better at it.</p>
</li>
</ul></div><div class="chapter-content"><h2>Credits</h2><p>We have used various freely-available resources to create this
project:</p><div class="asset-credits"><p class="credit-intro">For <code class="asset-filename">apple.png</code>, <code class="asset-filename">orange.png</code> (used in the project):</p><div class="credits"><p>The apple and orange images are copyright the Lifelong Kindergarten
Group at MIT Media Lab, and are part of Scratch.  We use them under
the terms of the <a href="https://creativecommons.org/licenses/by-sa/2.0/">Creative Commons Attribution-ShareAlike
2.0</a> licence.  We
have re-rendered the originals at a size suitable for our project.</p><p>Scratch is developed by the Lifelong Kindergarten Group at the MIT
Media Lab.  See <a href="https://scratch.mit.edu/">https://scratch.mit.edu/</a>.</p></div><p class="credit-intro">For <code class="asset-filename">solid-green.png</code> (used in the project):</p><div class="credits"><p>The solid green rectangle was created by the Pytch team and is hereby
placed into the public domain.</p></div><p class="credit-intro">For <code class="asset-filename">screenshot.png</code> (used in the tutorial text/summary):</p><div class="credits"><p>The screenshot was created by the Pytch team.  To the extent that it
includes material from Scratch, this material is used under the terms
of the <a href="https://creativecommons.org/licenses/by-sa/2.0/">Creative Commons Attribution-ShareAlike
2.0</a> licence.</p><p>Scratch is developed by the Lifelong Kindergarten Group at the MIT
Media Lab.  See <a href="https://scratch.mit.edu/">https://scratch.mit.edu/</a>.</p></div></div></div></div>